In [1]:
import sqlite3
In [2]:
conn= sqlite3.connect('ibrary_data.sqlite3')
In [3]:
cur= conn.cursor()
In [4]:
cur.execute('CREATE TABLE books(id integer primary key, name text,isbn text)')
In [5]:
cur.execute('INSERT INTO books(id,name,isbn)VALUES(1,"Basic Mathematics","abc-123")')
In [7]:
cur.fetchall()
Out[7]:
In [8]:
cur.execute('SELECT * FROM books')
Out[8]:
In [9]:
cur.fetchall()
Out[9]:
In [10]:
cur.execute('CREATE TABLE students(id integer primary key, name text, faculty text)')
In [11]:
cur.execute('INSERT INTO students(id,name,faculty)VALUES(71086,"Suravi Regmi","BCT")')
In [12]:
cur.execute('SELECT * FROM students')
cur.fetchall()
Out[12]:
In [13]:
cur.execute('INSERT INTO students(id,name,faculty)VALUES(71086,"Suravi Regmi","BCT")')
In [14]:
cur.execute('DELETE from books')
cur.fetchall()
Out[14]:
In [15]:
conn.commit()
In [16]:
cur.execute('CREATE TABLE teachers(id integer primary key, name text,faculty text)')
In [17]:
cur.execute('CREATE TABLE staff(id integer primary key, name text)')
In [18]:
sql='''INSERT INTO books (id,name,isbn) VALUES (?,?,?)'''
cur.executemany(sql,[(2,'Programming With C','a-1234'),
(3,'Basic Electrical Engineering','bee-865'),
(4,'Applied Mechanics','appm-986')])
Out[18]:
In [19]:
cur.execute('SELECT * FROM books')
cur.fetchall()
Out[19]:
In [20]:
cur.execute('DROP TABLE books')
Out[20]:
In [21]:
class Book:
def __init__(self,bid,name,isbn):
self.bid = bid
self.name = name
self.isbn = isbn
def save(self):
if self.id:
cur.execute('UPDATE books SET name =?, isbn =? WHERE id =? ',(self.name, self.isbn,self.id))
else:
cur.execute('INSERT INTO books(id,name,isbn)VALUES(?,?,?)',(self.bid, self.name, self.isbn))
@staticmethod
def get_book_by_name(name):
cur.execute('SELECT * FROM books WHERE name LIKE"%?%"', (name,))
return cur.fetchall()
@staticmethod
def get_all_books():
pass
@staticmethod
def get_book_by_id(_id):
cur.execute ('SELECT * FROM books WHERE id=?',(_id,))
result = cur.fetchone()
if resut:
book = Book()
book.id, book.name,book.isbn = result
return book
return None
In [ ]:
Book.get_book_by_name('Learning')
In [ ]:
conn.commit()
In [ ]:
book1=Book(5,'learn nepali','ln-145')
book1.save()
In [ ]:
book2 = Book.get_book_by_id(4)
book2.name = 'New Name'
book2.save()
In [ ]:
cur.execute('SELECT * FROM books')
cur.fetchall()
In [ ]:
class Student:
def __init__(self,stid,name,faculty):
self.stid = stid
self.name = name
self.faculty = faculty
def save(self):
cur.execute('INSERT INTO students(id,name,faculty)VALUES(?,?,?)',(self.stid, self.name, self.faculty))
def get_book_by_name(self):
cur.execute('SELECT * FROM books WHERE name LIKE"%?%"', (name,))
return cur.fetchall()
class Temperature:
temp=10
def __init__(self, temp):
self.temp =temp
def get_temperature(self):
return self.temp
@classmethod
def get_class_temperature(cls):
return cls.temp
@staticmethod
def get_added_temparature(inst):
return inst.temp + 10
@staticmethod
def get_added_temparature(inst):
return inst.temp + Temperature.temp
t =Temperature(20)
# t=Temperature()--> Temperature.__init__(t,20)
t.get_temperature() #20
# Temperature.get_temperature(t)
Temperature.get_class_temperature()
#Temperature.get_class_temperature(Temperature) 10
Temperature.get_added temperature(t)
#Temperature.get_added temperature(t)# 30
In [ ]: